﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="PairManager.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Collision.
//
//   starLiGHT.Collision is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Collision is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Collision. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 3231                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-01-11 21:44:51 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Collision
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using starLiGHT;
    using starLiGHT.Collections;

    #endregion

    public class PairManager<T>
    {
        #region Member Variables
        private Dictionary<int, UserPair<T>> activePairs = new Dictionary<int, UserPair<T>>();
        private bool locked;
        private FastList<int> pairsToRemove = new FastList<int>();

        #endregion

        public IEnumerable<UserPair<T>> Pairs
        {
            get
            {
                this.locked = true;

                foreach (UserPair<T> pair in this.activePairs.Values)
                {
                    yield return pair;
                }

                this.locked = false;

                int removeCount = this.pairsToRemove.Count;
                if (removeCount > 0)
                {
                    for (int i = 0; i < this.pairsToRemove.Count; i++)
                    {
                        this.activePairs.Remove(this.pairsToRemove[i]);
                    }

                    this.pairsToRemove.Clear();
                }
            }
        }

        public int Count
        {
            get { return this.activePairs.Count; }
        }

        public UserPair<T> AddPair(int id0, int id1, T object0, T object1)
        {
            if (this.locked)
            {
                throw new InvalidOperationException("PairManager is locked...");
            }

            UserPair<T> pair;
            int hash = this.Hash(id0, id1);

            if (!this.activePairs.TryGetValue(hash, out pair))
            {
                pair = new UserPair<T>(id0, id1, object0, object1);
                this.activePairs.Add(hash, pair);
            }

            return pair;
        }

        public bool RemovePair(int id0, int id1)
        {
            int hash = this.Hash(id0, id1);

            if (this.locked)
            {
                this.pairsToRemove.Add(hash);
                return true;
            }
            else
            {
                return this.activePairs.Remove(hash);
            }
        }

        private int Hash(int id0, int id1)
        {
            int key = (id1 << 16) | id0;
            key = ~key + (key << 15);
            key = key ^ (key >> 12);
            key = key + (key << 2);
            key = key ^ (key >> 4);
            key = key * 2057;
            key = key ^ (key >> 16);
            return key;
        }    
    }
}
